<template>
    <el-tabs class="tabs" v-model="activeName" @tab-click="handleClick">
        <el-tab-pane label="柜参数" name="first">
            <el-form
                class="boxForm"
                ref="boxForm"
                :model="boxForm"
                label-width="150px"
                size="mini"
                v-loading="tab1Loading"
                element-loading-background="rgba(0, 0, 0, 0.08)"
                element-loading-text="数据加载中"
                element-loading-spinner="el-icon-loading"
            >
                <div class="operate-btns text-right">
                    <el-button
                        size="mini"
                        type="success"
                        @click="doRefresh"
                        :loading="tab1RefreshLoading"
                        >手动刷新</el-button
                    >
                    <el-button
                        type="primary"
                        size="mini"
                        @click="isSetting"
                        :loading="tab1SettingLoading"
                        >设置</el-button
                    >
                </div>
                <el-form-item label="快充">
                    <el-switch
                        v-model="boxForm['06010014']"
                        active-value="1"
                        inactive-value="0"
                    >
                    </el-switch>
                </el-form-item>
                <el-form-item label="备电">
                    <el-switch
                        v-model="boxForm['06010015']"
                        active-value="1"
                        inactive-value="0"
                    >
                    </el-switch>
                </el-form-item>
                <el-form-item label="整流单元休眠">
                    <el-switch
                        v-model="boxForm['06010023']"
                        active-value="1"
                        inactive-value="0"
                    >
                    </el-switch>
                </el-form-item>
                <el-form-item label="电子锁反馈类型">
                    <el-select
                        clearable
                        v-model="boxForm['06010007']"
                        placeholder="请选择电子锁反馈类型"
                    >
                        <el-option
                            v-for="item in getLockOption"
                            :key="item.code"
                            :label="item.label"
                            :value="item.code"
                        >
                        </el-option>
                    </el-select>
                </el-form-item>
                <el-form-item label="门轴">
                    <el-select
                        style="width: 60%"
                        clearable
                        :disabled="!boxForm['06010008']"
                        v-model="boxForm['06010009']"
                        placeholder="请选择门轴"
                    >
                        <el-option
                            v-for="item in getLockOption"
                            :key="item.code"
                            :label="item.label"
                            :value="item.code"
                        >
                        </el-option>
                    </el-select>
                    <el-tooltip
                        :content="`门轴使能：${
                            boxForm['06010008'] == '1' ? '使能' : '禁用'
                        }`"
                        placement="top"
                        style="margin-left: 20px"
                    >
                        <el-switch
                            v-model="boxForm['06010008']"
                            active-value="1"
                            inactive-value="0"
                        >
                        </el-switch>
                    </el-tooltip>
                </el-form-item>

                <el-form-item label="电池在位模式">
                    <el-select
                        clearable
                        v-model="boxForm['06010011']"
                        placeholder="请选择电池在位模式"
                    >
                        <el-option
                            v-for="item in batteryIsOn"
                            :key="item.code"
                            :label="item.label"
                            :value="item.code"
                        >
                        </el-option>
                    </el-select>
                </el-form-item>
                <el-form-item label="灭火器反馈类型">
                    <el-select
                        clearable
                        v-model="boxForm['06010022']"
                        placeholder="请选择灭火器反馈类型"
                    >
                        <el-option
                            v-for="item in getLockOption"
                            :key="item.code"
                            :label="item.label"
                            :value="item.code"
                        >
                        </el-option>
                    </el-select>
                </el-form-item>
                <el-form-item label="主控2.0/3.0版本">
                    <el-select
                        v-model="boxForm['06010006']"
                        clearable
                        placeholder="请选择主控2.0/3.0版本"
                    >
                        <el-option
                            v-for="item in mainControlVersion"
                            :key="item.code"
                            :label="item.label"
                            :value="item.code"
                        >
                        </el-option>
                    </el-select>
                </el-form-item>
                <el-form-item label="消防模式">
                    <el-select
                        v-model="boxForm['06010024']"
                        clearable
                        placeholder="请选择消防模式"
                    >
                        <el-option
                            v-for="item in getLockOption"
                            :key="item.code"
                            :label="item.label"
                            :value="item.code"
                        >
                        </el-option>
                    </el-select>
                </el-form-item>
                <el-form-item label="系统启动散热温度(℃)">
                    <el-input-number
                        class="el-input"
                        :step="1"
                        :precision="0"
                        controls-position="right"
                        v-model="boxForm['06010016']"
                        placeholder="系统启动散热温度"
                        :min="0"
                        :max="80"
                    ></el-input-number>
                </el-form-item>
                <el-form-item label="从柜数量">
                    <el-input-number
                        :step="1"
                        class="el-input"
                        :precision="0"
                        controls-position="right"
                        v-model="boxForm['06010012']"
                        placeholder="请输入从柜数量"
                        :min="0"
                    ></el-input-number>
                </el-form-item>
                <el-form-item label="系统停止散热温度(℃)">
                    <el-input-number
                        :step="1"
                        class="el-input"
                        :precision="0"
                        controls-position="right"
                        v-model="boxForm['06010017']"
                        placeholder="系统停止散热温度"
                        :min="0"
                        :max="80"
                    ></el-input-number>
                </el-form-item>
                <el-form-item label="格口数量">
                    <el-input-number
                        :step="1"
                        class="el-input"
                        :precision="0"
                        controls-position="right"
                        v-model="boxForm['06010013']"
                        placeholder="请输入格口数量"
                        :min="0"
                    ></el-input-number>
                </el-form-item>
                <el-form-item label="启动整柜加热温度(℃)">
                    <el-input-number
                        :step="1"
                        class="el-input"
                        :precision="0"
                        controls-position="right"
                        v-model="boxForm['06010018']"
                        placeholder="启动整柜加热温度"
                        :min="0"
                        :max="50"
                    ></el-input-number>
                </el-form-item>
                <el-form-item label="设备SN码">
                    <el-input
                        v-model="boxForm['06010025']"
                        clearable
                        placeholder="请输入设备SN码"
                    ></el-input>
                </el-form-item>
                <el-form-item label="停止整柜加热温度(℃)">
                    <el-input-number
                        :step="1"
                        class="el-input"
                        :precision="0"
                        controls-position="right"
                        v-model="boxForm['06010019']"
                        placeholder="停止整柜加热温度"
                        :min="0"
                        :max="50"
                    ></el-input-number>
                </el-form-item>
                <el-form-item label="广告链接">
                    <el-input
                        v-model="boxForm['06010026']"
                        clearable
                        placeholder="请输入广告链接"
                    ></el-input>
                </el-form-item>
                <el-form-item label="有序充电">
                    <el-input
                        v-model="boxForm['06010027']"
                        clearable
                        placeholder="请输入有序充电"
                    ></el-input>
                </el-form-item>
                <el-form-item label="启动单仓加热温度(℃)">
                    <el-input-number
                        :step="1"
                        class="el-input"
                        :precision="0"
                        controls-position="right"
                        v-model="boxForm['06010020']"
                        placeholder="启动单仓加热温度"
                        :min="0"
                        :max="50"
                    ></el-input-number>
                </el-form-item>
                <el-form-item label="电池容量区分">
                    <el-switch
                        v-model="boxForm['06010028']"
                        active-value="1"
                        inactive-value="0"
                    >
                    </el-switch>
                </el-form-item>
                <el-form-item label="停止单仓加热温度(℃)">
                    <el-input-number
                        :step="1"
                        class="el-input"
                        :precision="0"
                        controls-position="right"
                        v-model="boxForm['06010021']"
                        placeholder="停止单仓加热温度"
                        :min="0"
                        :max="50"
                    ></el-input-number>
                </el-form-item>
                <!-- <el-form-item label="邮政密码">
          <el-input
            v-model="boxForm['06010029']"
            placeholder="请输入邮政密码"
          ></el-input>
        </el-form-item> -->
                <el-form-item label="满电阈值(%)">
                    <el-input-number
                        :step="1"
                        class="el-input"
                        :precision="0"
                        controls-position="right"
                        v-model="boxForm['06010031']"
                        placeholder="请输入满电阈值"
                        :min="1"
                        :max="100"
                    ></el-input-number>
                </el-form-item>
                <el-form-item label="管理员密码">
                    <el-input
                        v-model.trim="boxForm['06010030']" @input="boxForm['06010030'] = boxForm['06010030'].replace(/[\u4E00-\u9FA5]/g,'')"
                        clearable
                        placeholder="请输入管理员密码"
                    ></el-input>
                </el-form-item>

                <el-form-item label="输入连接器阻抗阈值">
                    <el-input-number
                        :step="1"
                        class="el-input"
                        :precision="0"
                        controls-position="right"
                        v-model="boxForm['06010064']"
                        placeholder="输入连接器阻抗阈值"
                        :min="1"
                    ></el-input-number>
                </el-form-item>

                <el-form-item label="喇叭音量设置">
                    <el-slider
                        v-model="boxForm['06010032']"
                        style="width: 58%"
                    ></el-slider>
                </el-form-item>

                <el-form-item label="输出连接器阻抗阈值">
                    <el-input-number
                        :step="1"
                        class="el-input"
                        :precision="0"
                        controls-position="right"
                        v-model="boxForm['06010065']"
                        placeholder="输出连接器阻抗阈值"
                        :min="1"
                    ></el-input-number>
                </el-form-item>
            </el-form>
        </el-tab-pane>
        <el-tab-pane label="格口" name="second">
            <el-checkbox v-model="secondChecked" @change="secondCheckedChange"
                >全部一致</el-checkbox
            >
            <el-divider></el-divider>

            <div>
                格口数量：{{ binNum }}
                <el-switch v-model="binSwitch" class="binSwitch"> </el-switch>
                <i class="el-icon-question"></i>
                <span>开关打开后，点击刷新会先读取设备格口数量</span>

                <el-button
                    size="mini"
                    plain
                    :loading="binNumLoading"
                    class="refresh-btn"
                    @click="doRefreshBin"
                    icon="el-icon-refresh"
                    >刷新</el-button
                >
            </div>
            <el-form
                class="binForm"
                ref="binForm"
                :model="binForm"
                label-width="80px"
                size="mini"
                v-loading="tab1Loading"
                element-loading-background="rgba(0, 0, 0, 0.08)"
                element-loading-text="数据加载中"
                element-loading-spinner="el-icon-loading"
            >
                <el-card v-for="(one, index) in binForm.entitys" :key="index">
                    <el-button
                        type="primary"
                        class="binIndex-btn"
                        :circle="true"
                        >{{ index + 1 }}</el-button
                    >
                    <div class="bin-form">
                        <el-form-item label="运行模式">
                            <el-select
                                clearable
                                @change="secondCheckedChange"
                                v-model="one['06010044']"
                                placeholder="请选择运行模式"
                                :disabled="secondChecked && index > 0"
                            >
                                <el-option
                                    v-for="item in runState"
                                    :disabled="item.readonly"
                                    :key="item.code"
                                    :label="item.label"
                                    :value="item.code"
                                >
                                </el-option>
                            </el-select>
                        </el-form-item>
                        <el-form-item label="备电模式">
                            <el-select
                                clearable
                                @change="secondCheckedChange"
                                v-model="one['06010045']"
                                placeholder="请选择备电模式"
                                :disabled="secondChecked && index > 0"
                            >
                                <el-option
                                    v-for="item in electricMode"
                                    :key="item.code"
                                    :label="item.label"
                                    :value="item.code"
                                >
                                </el-option>
                            </el-select>
                        </el-form-item>
                        <el-form-item label="充电电压">
                            <el-input
                                clearable
                                @blur="secondCheckedChange"
                                v-model="one['06010046']"
                                placeholder="请输入充电电压"
                                :disabled="secondChecked && index > 0"
                            ></el-input>
                        </el-form-item>
                        <el-form-item label="充电电流">
                            <el-select
                                clearable
                                @change="secondCheckedChange"
                                v-model="one['06010047']"
                                placeholder="请选择充电电流"
                                :disabled="secondChecked && index > 0"
                            >
                                <el-option
                                    v-for="item in electricJuice"
                                    :key="item.code"
                                    :label="item.label"
                                    :value="item.code"
                                >
                                </el-option>
                            </el-select>
                        </el-form-item>
                        <el-form-item label="电池类型">
                            <el-select
                                clearable
                                @change="secondCheckedChange"
                                v-model="one['06010048']"
                                placeholder="请选择电池类型"
                                :disabled="secondChecked && index > 0"
                            >
                                <el-option
                                    v-for="item in batteryType"
                                    :key="item.code"
                                    :label="item.label"
                                    :value="item.code"
                                >
                                </el-option>
                            </el-select>
                        </el-form-item>

                        <el-form-item label="气溶胶">
                            <el-select
                                clearable
                                @change="secondCheckedChange"
                                v-model="one['06010063']"
                                placeholder="请选择气溶胶"
                                :disabled="secondChecked && index > 0"
                            >
                                <el-option
                                    v-for="item in aerosol"
                                    :key="item.code"
                                    :label="item.label"
                                    :value="item.code"
                                >
                                </el-option>
                            </el-select>
                        </el-form-item>

                        <el-form-item label="邮政密码">
                            <el-input
                                clearable
                                @blur="secondCheckedChange"
                                v-model.trim="one['06010029']" @input="one['06010029'] = one['06010029'].replace(/[\u4E00-\u9FA5]/g,'')"
                                placeholder="请输入邮政密码"
                                :disabled="secondChecked && index > 0"
                            ></el-input>
                        </el-form-item>
                    </div>
                    <div class="operate-btns">
                        <el-button
                            size="mini"
                            type="success"
                            :disabled="secondChecked && index > 0"
                            :loading="one[`binRefresh${index + 1}`]"
                            @click="doRefresh(one, index)"
                            >手动刷新</el-button
                        >
                        <el-button
                            type="primary"
                            :disabled="secondChecked && index > 0"
                            size="mini"
                            :loading="one[`binSetting${index + 1}`]"
                            @click="isSetting(one, index)"
                            >设置</el-button
                        >
                    </div>
                </el-card>
            </el-form>
        </el-tab-pane>
        <!-- <el-tab-pane label="运维平台链接参数" name="third">
      <el-form
        class="boxForm"
        ref="maintenForm"
        :model="maintenForm"
        label-width="80px"
      >
        <el-form-item label="服务地址">
          <el-input
            v-model="maintenForm['06010049']"
            placeholder="请输入服务地址"
          ></el-input>
        </el-form-item>
        <el-form-item label="端口">
          <el-input
            v-model="maintenForm['06010050']"
            placeholder="请输入端口"
          ></el-input>
        </el-form-item>
        <el-form-item label="用户名">
          <el-input
            v-model="maintenForm.name"
            placeholder="请输入用户名"
          ></el-input>
        </el-form-item>
        <el-form-item label="密码">
          <el-input
            v-model.trim="maintenForm['06010030']" @input="maintenForm['06010030'] = maintenForm['06010030'].replace(/[\u4E00-\u9FA5]/g,'')"
            placeholder="请输入密码"
          ></el-input>
        </el-form-item>
        <el-form-item label="产品密钥">
          <el-input
            v-model.trim="maintenForm.name" @input="maintenForm.name = maintenForm.name.replace(/[\u4E00-\u9FA5]/g,'')"
            placeholder="请输入产品密钥"
          ></el-input>
        </el-form-item>
        <el-form-item label="设备名称">
          <el-input
            v-model="maintenForm.name"
            placeholder="请输入设备名称"
          ></el-input>
        </el-form-item>
        <el-form-item label="设备密钥">
          <el-input
            v-model.trim="maintenForm.name" @input="maintenForm.name = maintenForm.name.replace(/[\u4E00-\u9FA5]/g,'')"
            placeholder="请输入设备密钥"
          ></el-input>
        </el-form-item>
        <div class="operate-btns">
          <el-button size="mini" type="success" @click="doRefresh"
            >手动刷新</el-button
          >
          <el-button type="primary" size="mini">设置</el-button>
        </div>
      </el-form>
    </el-tab-pane> -->
        <el-tab-pane label="运营平台链接参数" name="fourth">
            <el-form
                class="boxForm operateForm"
                ref="operateForm"
                :rules="operateFormRule"
                :model="operateForm"
                label-width="80px"
                size="mini"
                v-loading="tab1Loading"
                element-loading-background="rgba(0, 0, 0, 0.08)"
                element-loading-text="数据加载中"
                element-loading-spinner="el-icon-loading"
            >
                <el-form-item label="服务地址" prop="url">
                    <el-input
                        clearable
                        v-model="operateForm.url"
                        placeholder="请输入服务地址"
                    ></el-input>
                </el-form-item>
                <el-form-item label="端口" prop="port">
                    <el-input
                        clearable
                        v-model="operateForm.port"
                        placeholder="请输入端口"
                    ></el-input>
                </el-form-item>
                <el-form-item label="用户名" prop="username">
                    <el-input
                        clearable
                        v-model="operateForm.username"
                        placeholder="请输入用户名"
                    ></el-input>
                </el-form-item>
                <el-form-item label="密码" prop="password">
                    <el-input
                        clearable
                        v-model.trim="operateForm.password" @input="operateForm.password = operateForm.password.replace(/[\u4E00-\u9FA5]/g,'')"
                        placeholder="请输入密码"
                    ></el-input>
                </el-form-item>
                <el-form-item label="产品密钥" prop="productKey">
                    <el-input
                        clearable
                        v-model.trim="operateForm.productKey" @input="operateForm.productKey = operateForm.productKey.replace(/[\u4E00-\u9FA5]/g,'')"
                        placeholder="请输入产品密钥"
                    ></el-input>
                </el-form-item>
                <el-form-item label="设备名称" prop="deviceName">
                    <el-input
                        clearable
                        v-model="operateForm.deviceName"
                        placeholder="请输入设备名称"
                    ></el-input>
                </el-form-item>
                <el-form-item label="设备密钥" prop="deviceSecret">
                    <el-input
                        clearable
                        v-model.trim="operateForm.deviceSecret" @input="operateForm.deviceSecret = operateForm.deviceSecret.replace(/[\u4E00-\u9FA5]/g,'')"
                        placeholder="请输入设备密钥"
                    ></el-input>
                </el-form-item>
                <div class="operate-btns">
                    <el-button
                        size="mini"
                        type="success"
                        @click="doRefresh"
                        :loading="tab4RefreshLoading"
                        >手动刷新</el-button
                    >
                    <el-button
                        type="primary"
                        size="mini"
                        :loading="tab4SettingLoading"
                        @click="doRunParam"
                        >设置</el-button
                    >
                </div>
            </el-form>
        </el-tab-pane>
        <el-tab-pane label="远程控制" name="fifth">
            <el-form
                class="controlForm"
                ref="controlForm"
                :rules="controlFormRule"
                :model="controlForm"
                label-width="110px"
                size="mini"
            >
                <el-form-item label="远程控制" prop="value">
                    <el-switch
                        v-model="controlForm.value"
                        :active-value="1"
                        :inactive-value="0"
                    >
                    </el-switch>
                </el-form-item>
                <el-form-item label="服务地址" prop="serverAddr">
                    <el-input
                        clearable
                        v-model="controlForm.serverAddr"
                        placeholder="请输入服务地址"
                    ></el-input>
                </el-form-item>
                <el-form-item label="服务端口" prop="serverPort">
                    <el-input-number
                        controls-position="right"
                        v-model="controlForm.serverPort"
                        :min="0"
                        placeholder="请输入服务端口"
                        class="el-input"
                    ></el-input-number>
                </el-form-item>
                <el-form-item label="远端映射端口" prop="remotePort">
                    <el-input-number
                        controls-position="right"
                        v-model="controlForm.remotePort"
                        :min="0"
                        placeholder="请输入远端映射端口"
                        class="el-input"
                    ></el-input-number>
                </el-form-item>
            </el-form>
            <div class="operate-btns">
                <el-button
                    type="primary"
                    size="mini"
                    @click="isSetControl"
                    :loading="tab5RefreshLoading"
                    >设置</el-button
                >
            </div>
        </el-tab-pane>
    </el-tabs>
</template>
<script>
import { equipment } from "@/api/factuer";
import {
    getLockOption,
    batteryIsOn,
    mainControlVersion,
    runState,
    electricMode,
    electricJuice,
    batteryType,
    aerosol,
} from "@/utils/config";
import { handleConfirm } from "@/utils/messageBox";
import { removeNullUndefined } from "@/utils/index.js";

export default {
    props: {
        equipId: {
            type: String,
            default: "",
        },
        visible: {
            type: Boolean,
            default: true,
        },
    },
    data() {
        return {
            getLockOption: getLockOption,
            batteryIsOn: batteryIsOn,
            mainControlVersion: mainControlVersion,
            runState: runState,
            electricMode: electricMode,
            electricJuice: electricJuice,
            batteryType: batteryType,
            aerosol: aerosol,

            activeName: "first",
            //   柜参数
            boxForm: {
                "06010014": undefined,
                "06010015": undefined,
                "06010023": undefined,
                "06010007": undefined,
                "06010008": undefined,
                "06010009": undefined,
                "06010011": undefined,
                "06010019": undefined,
                "06010022": undefined,
                "06010006": undefined,
                "06010024": undefined,
                "06010012": undefined,
                "06010013": undefined,
                "06010016": undefined,
                "06010017": undefined,
                "06010018": undefined,
                "06010025": undefined,
                "06010027": undefined,
                "06010028": undefined,
                "06010020": undefined,
                "06010021": undefined,
                // "06010029": undefined,
                "06010031": undefined,
                "06010030": undefined,
                "06010032": undefined,
                "06010026": undefined,
                "06010064": undefined, //输入连接器阻抗阈值
                "06010065": undefined, //输出连接器阻抗阈值
            },
            binForm: {
                entitys: [],
            },
            secondChecked: false, //全部一致
            controlForm: {
                remotePort: undefined,
                serverAddr: undefined,
                serverPort: undefined,
                value: 0,
            },
            controlFormRule: {
                value: [
                    {
                        required: true,
                        message: "请选择远程控制",
                        trigger: "change",
                    },
                ],
                remotePort: [
                    {
                        required: true,
                        message: "请输入远端映射端口",
                        trigger: "change",
                    },
                ],
                serverAddr: [
                    {
                        required: true,
                        message: "请输入服务地址",
                        trigger: "blur",
                    },
                ],
                serverPort: [
                    {
                        required: true,
                        message: "请输入服务端口",
                        trigger: "change",
                    },
                ],
            },
            operateFormRule: {
                url: [
                    {
                        required: true,
                        message: "请输入服务地址",
                        trigger: "blur",
                    },
                ],
                port: [
                    { required: true, message: "请输入端口", trigger: "blur" },
                ],
                username: [
                    {
                        required: true,
                        message: "请输入用户名",
                        trigger: "blur",
                    },
                ],
                password: [
                    { required: true, message: "请输入密码", trigger: "blur" },
                ],
                productKey: [
                    {
                        required: true,
                        message: "请输入产品密钥",
                        trigger: "blur",
                    },
                ],
                deviceName: [
                    {
                        required: true,
                        message: "请输入设备名称",
                        trigger: "blur",
                    },
                ],
                deviceSecret: [
                    {
                        required: true,
                        message: "请输入设备密钥",
                        trigger: "blur",
                    },
                ],
            },
            maintenForm: {
                "06010049": undefined,
                "06010030": undefined,
            },
            operateForm: {
                deviceName: "",
                deviceSecret: "",
                password: "",
                port: "",
                productKey: "",
                url: "",
                username: "",
            },
            params: {
                entitys: [],
                equipId: this.equipId,
            },
            tab1RefreshLoading: false,
            tab1SettingLoading: false,
            tab4SettingLoading: false,
            tab1Loading: false,
            tab5RefreshLoading: false,
            tab4RefreshLoading: false,
            binNum: null, //格口数量
            binNumLoading: false,
            enumsJson: {},
            binSwitch: false,
        };
    },

    watch: {
        equipId: {
            handler: function (value) {},
            immediate: true,
        },
        visible: {
            handler: function (value) {
                if (!value) {
                    this.resetDialog();
                } else {
                    this.activeName = "first";
                    this.doRead();
                }
            },
            immediate: true,
        },
    },

    methods: {
        // 重置dialog数据
        resetDialog() {
            this.binForm = {
                entitys: [],
            };
            this.params.entitys = [];
            for (const key in this.boxForm) {
                this.boxForm[key] = undefined;
            }
            for (const key in this.operateForm) {
                this.operateForm[key] = undefined;
            }
            for (const key in this.controlForm) {
                this.controlForm[key] = undefined;
            }
            this.binNum = null;
            this.binSwitch = false;
            this.secondChecked = false;

            this.tab1RefreshLoading = false;
            this.tab1SettingLoading = false;
            this.tab4SettingLoading = false;
            this.tab1Loading = false;
            this.tab5RefreshLoading = false;
            this.tab4RefreshLoading = false;
            this.binNumLoading = false;
        },
        handleClick(tab, event) {
            this.activeName = tab.name;
            switch (this.activeName) {
                case "first":
                    this.doRead();
                    break;
                case "second":
                    this.readBinIndex();
                    break;
                case "fourth":
                this.$refs.operateForm.resetFields();
                    this.doRead();
                    break;
                case "fifth":
                    this.$refs.controlForm.resetFields();
                    break;
            }
        },
        doRefresh(one, index) {
            handleConfirm(`确定要刷新吗？`, "warning", "提示")
                .then((res) => {
                    switch (this.activeName) {
                        case "first":
                            this.tab1RefreshLoading = true;
                            break;
                        case "fourth":
                            this.$refs["operateForm"].resetFields();
                            this.tab4RefreshLoading = true;
                            break;
                        case "fifth":
                            this.$refs["controlForm"].resetFields();
                            break;
                    }
                    this.doRead(one, index);
                })
                .catch((err) => {});
        },
        doRead(one, index) {
            this.tab1Loading = true;
            let params = {
                entitys: [],
                equipId: this.equipId,
            };
            switch (this.activeName) {
                case "first":
                    params.entitys = [];
                    for (const key in this.boxForm) {
                        params.entitys.push({ id: key });
                    }
                    break;
                case "second":
                    if (index + 1) {
                        //单个操作
                        params.entitys = [];
                        for (const key in one) {
                            params.entitys.push({ id: key, addrId: index + 1 });
                        }
                    } else {
                        params.entitys = [];
                        this.binForm.entitys.forEach((item, index) => {
                            for (const key in item) {
                                params.entitys.push({
                                    id: key,
                                    addrId: index + 1,
                                });
                            }
                        });
                    }
                    break;
                case "fourth":
                    params.entitys = [];
                    for (const key in this.operateForm) {
                        params.entitys = [
                            { id: "06010049" },
                            { id: "06010050" },
                        ];
                    }
                    break;
            }

            equipment("/operate/param/read", "post", params, 1)
                .then((res) => {
                    let datas = res.data;

                    switch (this.activeName) {
                        case "first":
                            for (const key in this.boxForm) {
                                datas.forEach((item) => {
                                    if (item.id == "06010032")
                                        item.value = +item.value;
                                    if (key == item.id) {
                                        this.boxForm[key] = item.value;
                                    }
                                });
                            }
                            break;
                        case "second":
                            this.binForm.entitys.map((pp, index) => {
                                for (const key in pp) {
                                    datas.map((item, inx) => {
                                        if (
                                            item.addrId == index + 1 &&
                                            item.id == key
                                        ) {
                                            pp[key] = datas[inx].value;
                                        }
                                    });
                                }
                            });
                            break;
                        case "fourth":
                            datas.forEach((item) => {
                                if (item.id == "06010049") {
                                    this.operateForm.url = item.value;
                                }
                                if (item.id == "06010050") {
                                    this.operateForm.port = item.value;
                                }
                            });
                            break;
                    }
                })
                .finally(() => {
                    this.tab1Loading = false;

                    switch (this.activeName) {
                        case "first":
                            this.tab1RefreshLoading = false;
                            break;
                        case "fourth":
                            this.tab4RefreshLoading = false;
                            break;
                    }
                });
        },
        doRefreshBin() {
            this.secondChecked = false;
            this.binNumLoading = this.binSwitch;

            this.binForm.entitys.forEach((pp, index) => {
                for (const key in pp) {
                    pp[key] = undefined;
                }
            });
            if (this.binSwitch) {
                this.readBinIndex();
            } else {
                this.doRead();
            }
        },
        // 读取格口数量
        readBinIndex() {
            this.binForm.entitys = [];
            equipment(
                `/operate/param/read/bincount/${this.equipId}`,
                "post",
                { refresh: this.binSwitch },
                2
            )
                .then((res) => {
                    this.binNum = res.data;
                    if (this.binNum) {
                        for (let i = 0; i < this.binNum; i++) {
                            let one = {
                                "06010044": undefined,
                                "06010045": undefined,
                                "06010046": undefined,
                                "06010047": undefined,
                                "06010048": undefined,
                                "06010029": undefined,
                                "06010063": undefined,
                            };
                            // one[`binRefresh${i + 1}`] = false;
                            // one[`binSetting${i + 1}`] = false;
                            this.binForm.entitys.push(one);
                        }
                        this.doRead();
                    }
                })
                .finally(() => (this.binNumLoading = false));
        },
        isSetting(one, index) {
            let params = {
                entitys: [],
                equipId: this.equipId,
            };
            switch (this.activeName) {
                case "first":
                    for (const key in this.boxForm) {
                        params.entitys.push({
                            id: key,
                            value: this.boxForm[key],
                        });
                    }
                    break;
                case "second":
                    for (const key in one) {
                        params.entitys.push({
                            id: key,
                            value: one[key],
                            addrId: index + 1,
                        });
                    }
                    break;
            }
            let datas = { ...params };
            datas.entitys = datas.entitys.filter((item) => item.value);
            if (datas.entitys.length == 0) {
                this.$message.error("请输入要设置的参数值！");
                return;
            }
            let msg =
                this.secondChecked && this.activeName == "second"
                    ? "确定要设置全部一致吗？"
                    : "确定要设置吗？";
            handleConfirm(`${msg}`, "warning", "提示")
                .then((res) => {
                    this.doSetting(one, index);
                })
                .catch((err) => {});
        },
        // 设置参数
        doSetting(one, index) {
            this.tab1Loading = true;
            this.tab1SettingLoading = true;
            let params = {
                entitys: [],
                equipId: this.equipId,
            };
            switch (this.activeName) {
                case "first":
                    for (const key in this.boxForm) {
                        params.entitys.push({
                            id: key,
                            value: this.boxForm[key],
                        });
                    }
                    break;
                case "second":
                    if (this.secondChecked) {
                        //全部一致
                        for (let i = 0; i < this.binNum; i++) {
                            for (const key in one) {
                                params.entitys.push({
                                    id: key,
                                    value: one[key],
                                    addrId: i + 1,
                                });
                            }
                        }
                    } else {
                        for (const key in one) {
                            params.entitys.push({
                                id: key,
                                value: one[key],
                                addrId: index + 1,
                            });
                        }
                    }
                    break;
            }
            let datas = { ...params };
            datas.entitys = datas.entitys.filter(
                (item) => item.value || item.value == "0"
            );

            equipment("/operate/param/write", "post", datas, 1)
                .then((res) => {
                    if (res.code == 200) {
                        this.$message.success(res.message);
                    }
                })
                .finally(() => {
                    this.tab1Loading = false;
                    this.tab1SettingLoading = false;
                });
        },
        // 远程控制
        isSetControl() {
            this.$refs["controlForm"].validate((valid) => {
                if (valid) {
                    handleConfirm(`确定要设置吗？`, "warning", "提示")
                        .then((res) => {
                            this.setRemoteControl();
                        })
                        .catch((err) => {});
                } else {
                    return false;
                }
            });
        },
        setRemoteControl() {
            this.tab5RefreshLoading = true;
            equipment(
                `/control/remotecontrol/${this.equipId}`,
                "post",
                this.controlForm,
                1
            )
                .then((res) => {
                    if (res.code == 200) {
                        this.$message.success(res.message);
                        this.$refs["controlForm"].resetFields();
                        this.$emit("cancelDialog");
                    }
                })
                .finally(() => {
                    this.tab5RefreshLoading = false;
                });
        },
        // 运营平台链接参数
        doRunParam() {
            this.$refs["operateForm"].validate((valid) => {
                if (valid) {
                    handleConfirm(`确定要设置吗？`, "warning", "提示")
                        .then((res) => {
                            this.tab4SettingLoading = true;
                            equipment(
                                `/control/op/connectionparam/${this.equipId}`,
                                "post",
                                { ...removeNullUndefined(this.operateForm) },
                                1
                            )
                                .then((res) => {
                                    if (res.code == 200) {
                                        this.$message.success(res.message);
                                    }
                                })
                                .finally(
                                    () => (this.tab4SettingLoading = false)
                                );
                        })
                        .catch((err) => {});
                } else {
                    return false;
                }
            });
        },
        secondCheckedChange() {
            if (this.binNum > 0 && this.secondChecked) {
                let obj = this.binForm.entitys[0];
                this.binForm.entitys.forEach((item, index) => {
                    if (index != 0) {
                        for (const key in this.binForm.entitys[index]) {
                            this.binForm.entitys[index][key] = obj[key];
                        }
                    }
                });
            }
        },
    },
};
</script>
<style lang="scss">
@import "@/styles/common.scss";
</style>

<style scoped lang="scss">
@import "@/styles/common.scss";
::v-deep .el-card__body {
    @include flexLayout(center, flex-start);
    flex-wrap: wrap;
    padding: 10px 0 10px 10px;
    .el-form-item {
        width: 50%;
        margin-bottom: 5px;
    }
}
.binForm {
    @include flexLayout(center, flex-start);
    flex-wrap: wrap;
}
// 屏幕分辨率
@media (min-width: 1441px) {
    .binForm {
        .el-card {
            width: 32%;
        }
        .el-card:not(:nth-child(3n)) {
            margin-right: 2%;
        }
    }
}

@media (max-width: 1440px) {
    .binForm {
        .el-card {
            width: 49%;
        }
        .el-card:nth-child(2n) {
            margin-left: 2%;
        }
    }
}
.boxForm {
    @include flexLayout(center, flex-start);
    flex-wrap: wrap;
    .el-form-item {
        width: 50%;
        margin-bottom: 10px;
    }
}

.operateForm {
    width:80%;
    margin:0 10%;
    .el-form-item {
        margin-bottom: 15px;
    }
}

.el-input,
.el-select {
    width: 200px;
}
.el-card {
    margin: 10px 0 2px;
}
.operate-btns {
    width: 100%;
    text-align: center;
}
.text-right {
    text-align: right;
}
.controlForm {
    margin-left: 30%;
    .el-form-item {
        margin-bottom: 15px;
    }
}
.refresh-btn {
    margin-left: 20px;
}
.bin-form {
    width: 85%;
    .el-input,
    .el-select {
        width: 170px;
    }
}
.binIndex-btn {
    padding: 0;
    width: 28px;
    height: 28px;
    line-height: 28px;
    text-align: center;
    background-color: #409eff;
    border-color: #409eff;
}
.refresh-btn {
    margin-left: 20px;
}
.el-icon-question {
    font-size: 20px;
    margin: 0 6px 0 10px;
    color: #f56c6c;
}
.binSwitch {
    margin: -5px 0 0 15px;
}
.el-divider--horizontal {
    margin: 6px 0;
}
::v-deep .el-input.is-disabled .el-input__inner {
    color: #333333;
}
</style>
